//headers 
#include<algorithm>
#include<cassert>
#include<cstdio>
#include<deque>
#include<iostream>
#include<list>
#include<map>
#include<numeric>
#include<deque>
#include<set>
#include<string>
#include<sstream>
#include<stack>
#include<streambuf>
#include<vector>
using namespace std;

//macros
#define LET(x,a) typeof(a) x(a)
#define FOR(i,a,n) for(LET(i,a);i!=n;i++)
#define REP(i,n) FOR(i,0,n)
#define sz size()
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define dbge(x) cout<<#x<<"->"<<x<<endl;
#define PRINT(x) REP(i,x.sz) dbge(x[i]);
#define OUT(x) x<<" "

#define VI vector<int>
#define VS vector<string>
#define LL long long

//code of suren

//prob: 1000 longest paths


int a[100][100];
int visited[100];
int end,max_value=0;
int N;

void dfs(int node, int length){
  if(length>max_value) { max_value=length; if(node<end) end=node; }
  REP(i,N) if( !visited[i] && a[node][i] ) { visited[i]=1; dfs(i,length+1); visited[i]=0; }
  return;
}

int main(){
  int cont; int num=1;
  while(cin>>cont && cont) {
    int n,start; cin>>start; N=cont; int i,j;
    memset(a,0,sizeof(a));
    memset(visited,0,sizeof(visited));

    while(cin>>i>>j && i && j ) a[i-1][j-1]=1;
    
    visited[start-1]=1;
    dfs(start-1,0);
    cout<<"Case "<<num<<": The longest path from "<<start<<" has length "<<max_value<<", finishing at "<<end<<"."<<endl;
    num++;
  }
  return 0;
}

